#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using IGIS.DAL;
using System.Data;
using System.Data.SqlClient;

#endregion

namespace IGIS.BOL
{
    public class Customer
    {
        private int intfldID;
        private int intCustomerID;
        private int intFK_KEVER;
        private int intHELKA;
        private string strStatus;
        private string strFname;
        private string strLname;
        private string strAddress;
        private int intYeshov;
        private int intZip;
        private string strDtBirth;
        private string strDtPast;
        private int intContact1;
        private int intContact1Type;
        private int intContact2;
        private int intContact3;
        private string strDtRehisha;
        private string strDtGiluiMazeva;
        private float fltPrice;
        private string strPayment;
        private string strPaymentType;
        private string strComment;
        private string strDtPeraoon;
        private int intLayoutID;

        public int LayoutID
        {
            get
            {
                return intLayoutID;
            }
            set
            {
                intLayoutID = value;
            }
        }

        public int fldID
        {
            get
            {
                return intfldID;
            }
            set
            {
                intfldID = value;
            }
        }

        public int CustomerID
        {
            get
            {
                return intCustomerID;
            }
            set
            {
                intCustomerID = value;
            }
        }

        public int FK_KEVER
        {
            get
            {
                return intFK_KEVER;
            }
            set
            {
                intFK_KEVER = value;
            }
        }

        public int HELKA
        {
            get
            {
                return intHELKA;
            }
            set
            {
                intHELKA = value;
            }
        }

        public string Status
        {
            get
            {
                return strStatus;
            }
            set
            {
                strStatus = value;
            }
        }

        public string Fname
        {
            get
            {
                return strFname;
            }
            set
            {
                strFname = value;
            }
        }

        public string Lname
        {
            get
            {
                return strLname;
            }
            set
            {
                strLname = value;
            }
        }

        public string Address
        {
            get
            {
                return strAddress;
            }
            set
            {
                strAddress = value;
            }
        }

        public int Yeshov
        {
            get
            {
                return intYeshov;
            }
            set
            {
                intYeshov = value;
            }
        }

        public int Zip
        {
            get
            {
                return intZip;
            }
            set
            {
                intZip = value;
            }
        }

        public string DtBirth
        {
            get
            {
                return strDtBirth;
            }
            set
            {
                strDtBirth = value;
            }
        }

        public string DtPast
        {
            get
            {
                return strDtPast;
            }
            set
            {
                strDtPast = value;
            }
        }

        public int Contact1
        {
            get
            {
                return intContact1;
            }
            set
            {
                intContact1 = value;
            }
        }

        public int Contact1Type
        {
            get
            {
                return intContact1Type;
            }
            set
            {
                intContact1Type = value;
            }
        }

        public int Contact2
        {
            get
            {
                return intContact2;
            }
            set
            {
                intContact2 = value;
            }
        }

        public int Contact3
        {
            get
            {
                return intContact3;
            }
            set
            {
                intContact3 = value;
            }
        }

        public string DtRehisha
        {
            get
            {
                return strDtRehisha;
            }
            set
            {
                strDtRehisha = value;
            }
        }

        public string DtGiluiMazeva
        {
            get
            {
                return strDtGiluiMazeva;
            }
            set
            {
                strDtGiluiMazeva = value;
            }
        }

        public float Price
        {
            get
            {
                return fltPrice;
            }
            set
            {
                fltPrice = value;
            }
        }

        public string DtPeraoon
        {
            get
            {
                return strDtPeraoon;
            }
            set
            {
                strDtPeraoon = value;
            }
        }

        public string Payment
        {
            get
            {
                return strPayment;
            }
            set
            {
                strPayment = value;
            }
        }

        public string PaymentType
        {
            get
            {
                return strPaymentType;
            }
            set
            {
                strPaymentType = value;
            }
        }

        public string Comment
        {
            get
            {
                return strComment;
            }
            set
            {
                strComment = value;
            }
        }

        public static int Insert(Customer c)
        {
            DBSQLExpress db = new DBSQLExpress();
            //DBMDBSpatial dbf = new DBMDBSpatial();

            SqlParameter objParam = new SqlParameter("@ifldID", 0);
            objParam.Direction = ParameterDirection.Output;
            
            db.Parameters.Add(new SqlParameter("@iCustomerID", c.CustomerID));
            db.Parameters.Add(new SqlParameter("@iFK_KEVER", c.FK_KEVER));
            db.Parameters.Add(new SqlParameter("@sFName", c.Fname));
            db.Parameters.Add(new SqlParameter("@sLName", c.Lname));
            db.Parameters.Add(new SqlParameter("@sAddress", c.Address));
            db.Parameters.Add(new SqlParameter("@iCity", c.Yeshov));
            db.Parameters.Add(new SqlParameter("@iZip", c.Zip));
            if (c.DtBirth != null)
                db.Parameters.Add(new SqlParameter("@dtBirth", c.DtBirth));
            if (c.DtPast != null)
                db.Parameters.Add(new SqlParameter("@dtPaste",Convert.ToDateTime(c.DtPast)));
            db.Parameters.Add(new SqlParameter("@iContact1", c.Contact1));
            db.Parameters.Add(new SqlParameter("@iContact1Type", c.Contact1Type));
            db.Parameters.Add(new SqlParameter("@iContact2", c.Contact2));
            db.Parameters.Add(new SqlParameter("@iContact3", c.Contact3));
            if (c.DtRehisha != null)
                db.Parameters.Add(new SqlParameter("@dtREHISHA",Convert.ToDateTime(c.DtRehisha)));
            if (c.DtGiluiMazeva != null)
                db.Parameters.Add(new SqlParameter("@dtGILOUIMAZEVA", c.DtGiluiMazeva));
            db.Parameters.Add(new SqlParameter("@mPrice", c.Price));
            db.Parameters.Add(new SqlParameter("@sPayment", c.Payment));
            db.Parameters.Add(new SqlParameter("@sPaymentType", c.PaymentType));
            db.Parameters.Add(new SqlParameter("@sCommants", c.Comment));
            if (c.DtPeraoon != null)
                db.Parameters.Add(new SqlParameter("@dtPayPeraoon", c.DtPeraoon));
            db.Parameters.Add(new SqlParameter("@iStatus", c.Status));
            db.Parameters.Add(new SqlParameter("@iUID", c.LayoutID));
            db.Parameters.Add(objParam);

            int retval = db.ExecuteNonQuery("Betolam_Customers_Insert");
            if (retval == 1)
            {
               // string restr = dbf.UpdateKeverStatus(int.Parse(c.Status), c.FK_KEVER, c.LayoutID);
                return 1;
            }
            else
            {
                return -1;
            }

        }
        public static int Update(Customer c)
        {
            DBSQLExpress db = new DBSQLExpress();
          //  DBMDBSpatial dbf = new DBMDBSpatial();

            db.Parameters.Add(new SqlParameter("@ifldID", c.fldID));
            db.Parameters.Add(new SqlParameter("@iCustomerID", c.CustomerID));
            db.Parameters.Add(new SqlParameter("@iFK_KEVER", c.FK_KEVER));
            //db.Parameters.Add(new SqlParameter("@iHELKA", c.HELKA));
            db.Parameters.Add(new SqlParameter("@sFName", c.Fname));
            db.Parameters.Add(new SqlParameter("@sLName", c.Lname));
            db.Parameters.Add(new SqlParameter("@sAddress", c.Address));
            db.Parameters.Add(new SqlParameter("@iCity", c.Yeshov));
            db.Parameters.Add(new SqlParameter("@iZip", c.Zip));
            if(c.DtBirth!="")
                db.Parameters.Add(new SqlParameter("@dtBirth", c.DtBirth));
            if (c.DtPast != "")
                db.Parameters.Add(new SqlParameter("@dtPaste", c.DtPast));
            db.Parameters.Add(new SqlParameter("@iContact1", c.Contact1));
            db.Parameters.Add(new SqlParameter("@iContact1Type", c.Contact1Type));
            db.Parameters.Add(new SqlParameter("@iContact2", c.Contact2));
            db.Parameters.Add(new SqlParameter("@iContact3", c.Contact3));
            if (c.DtRehisha != "")
                db.Parameters.Add(new SqlParameter("@dtREHISHA", Convert.ToDateTime(c.DtRehisha)));
            if (c.DtGiluiMazeva != "")
                db.Parameters.Add(new SqlParameter("@dtGILOUIMAZEVA", c.DtGiluiMazeva));
            db.Parameters.Add(new SqlParameter("@mPrice", c.Price));
            db.Parameters.Add(new SqlParameter("@sPayment", c.Payment));
            db.Parameters.Add(new SqlParameter("@sPaymentType", c.PaymentType));
            db.Parameters.Add(new SqlParameter("@sCommants", c.Comment));
            if (c.DtPeraoon != "")
                db.Parameters.Add(new SqlParameter("@dtPayPeraoon", c.DtPeraoon));
            db.Parameters.Add(new SqlParameter("@iStatus", c.Status));

            int retval = db.ExecuteNonQuery("Betolam_Customers_Update");
          //  string restr = dbf.UpdateKeverStatus(int.Parse(c.Status), c.FK_KEVER, c.LayoutID);
            
            return retval;
        }

        public Customer GetCustomersKeverID(string sqlWhere)
        {
            DBSQLExpress db = new DBSQLExpress();
            db.Parameters.Add(new SqlParameter("@namelist", sqlWhere));
            SqlDataReader dr = (SqlDataReader)db.ExecuteReader("Betolam_Customers_KeverByName");
            if (dr.HasRows)
            {
                Customer c = new Customer();
                while (dr.Read())
                {
            //        //c.CompanyID = dr.GetInt32(dr.GetOrdinal("companyid"));
            //        //c.CompanyName = dr.GetString(dr.GetOrdinal("CompanyName"));
            //        //c.BriefProfile = dr.GetString(dr.GetOrdinal("CompanyProfile"));
            //        //c.WebSiteUrl = dr.GetString(dr.GetOrdinal("WebSiteUrl"));
            //        //c.Address1 = dr.GetString(dr.GetOrdinal("Address1"));
            //        //c.Address2 = dr.GetString(dr.GetOrdinal("Address2"));
            //        //c.City = dr.GetString(dr.GetOrdinal("City"));
            //        //c.StateID = dr.GetInt32(dr.GetOrdinal("StateID"));
            //        //c.CountryID = dr.GetInt32(dr.GetOrdinal("CountryID"));
            //        //c.ZIP = dr.GetString(dr.GetOrdinal("ZIP"));
            //        //c.Phone = dr.GetString(dr.GetOrdinal("Phone"));
            //        //c.Fax = dr.GetString(dr.GetOrdinal("Fax"));
            //        //c.Email = dr.GetString(dr.GetOrdinal("CompanyEmail"));
            //        //c.UserName = dr.GetString(dr.GetOrdinal("UserName"));
                }
            //    dr.Close();
            //    return c;
            //}
            //else
            //{
            //    dr.Close();
                return null;
            }
            return null;
        }

        public static Customer GetCompany(int companyid)
        {
            DBSQLExpress db = new DBSQLExpress();
            db.Parameters.Add(new SqlParameter("@iCompanyID", companyid));
            SqlDataReader dr = (SqlDataReader)db.ExecuteReader("JobsDb_Companies_SelectOne");
            if (dr.HasRows)
            {
                Customer c = new Customer();
                while (dr.Read())
                {
                    //c.CompanyID = dr.GetInt32(dr.GetOrdinal("companyid"));
                    //c.CompanyName = dr.GetString(dr.GetOrdinal("CompanyName"));
                    //c.BriefProfile = dr.GetString(dr.GetOrdinal("CompanyProfile"));
                    //c.WebSiteUrl = dr.GetString(dr.GetOrdinal("WebSiteUrl"));
                    //c.Address1 = dr.GetString(dr.GetOrdinal("Address1"));
                    //c.Address2 = dr.GetString(dr.GetOrdinal("Address2"));
                    //c.City = dr.GetString(dr.GetOrdinal("City"));
                    //c.StateID = dr.GetInt32(dr.GetOrdinal("StateID"));
                    //c.CountryID = dr.GetInt32(dr.GetOrdinal("CountryID"));
                    //c.ZIP = dr.GetString(dr.GetOrdinal("ZIP"));
                    //c.Phone = dr.GetString(dr.GetOrdinal("Phone"));
                    //c.Fax = dr.GetString(dr.GetOrdinal("Fax"));
                    //c.Email = dr.GetString(dr.GetOrdinal("CompanyEmail"));
                    //c.UserName = dr.GetString(dr.GetOrdinal("UserName"));
                }
                dr.Close();
                return c;
            }
            else
            {
                dr.Close();
                return null;
            }
        }


        public DataSet GetCustomers_ByLayoutID(int layoutid)
        {
            DBSQLExpress db = new DBSQLExpress();
            db.AddParameter("@LayoutID", layoutid);
            return db.ExecuteDataSet("BetOlam_GetCustomersByLayoutID");
        }

        public static int GetCompanyCount()
        {
            DBSQLExpress db = new DBSQLExpress();
            return (int)db.ExecuteScalar("JobsDb_Companies_GetCount");
        }
    }
}
